<template>
  <div class="profile_message">
    <profile-title></profile-title>
    <scroll v-if="message.length > 0" class="content">
      <div>
        <message-item v-for="index in pageSize" :item-message="message[index - 1 + 10 * (currentPage - 1)]"></message-item>
      </div>
      <sorter :page-size="10"
              :total="message.length"
              @currentPageChange="currentPageChange"></sorter>
    </scroll>
    <div v-else class="no_message">还没有消息噢~快和小伙伴互动起来</div>
  </div>
</template>

<script>
  // 公共组件
  import Sorter from "components/content/sorter/Sorter";
  import Scroll from "components/content/scroll/Scroll";

  import ProfileTitle from "../ProfileTitle";
  import MessageItem from "./MessageItem";

  import {getMessage} from "network/profile";

  export default {
    name: "ProfileMessage",
    components: {
      Sorter,
      Scroll,
      ProfileTitle,
      MessageItem
    },
    data() {
      return {
        currentPage: 1,
        message: []
      }
    },
    created() {
      getMessage(this.$store.state.userInfo.id).then(res => {
        if(res.data.code) {
          this.message = res.data.data[0].list.reverse()
        }
      })
    },
    computed: {
      pageSize() {
        return this.currentPage < Math.ceil(this.message.length / 10) && this.message.length >= 10 ? 10 : this.message.length - (this.currentPage - 1) * 10
      }
    },
    methods: {
      // 事件监听
      currentPageChange(page) {
        this.currentPage = page
      },
    }
  }
</script>

<style scoped>
  .profile_message {
    position: relative;
  }

  .content {
    width: 790px;
    height: calc(100vh - 125px);
    background-color: #fff;
  }

  .no_message {
    margin-top: 60px;
    color: #ccc;
    font-size: 18px;
    text-align: center;
  }
</style>
